package com.atguigu.gmallre.mock.stage;

import com.atguigu.gmallre.mock.service.LogService;
import com.atguigu.mock.util.LogUtil;
import com.atguigu.mock.util.RandomBox;
import com.atguigu.mock.util.RandomCollection;
import com.atguigu.gmallre.mock.bean.CartInfo;
import com.atguigu.gmallre.mock.config.AppConfig;
import com.atguigu.gmallre.mock.factory.UserSession;
import com.atguigu.gmallre.mock.log.*;
import com.atguigu.gmallre.mock.log.enums.ActionId;
import com.atguigu.gmallre.mock.log.enums.ItemType;
import com.atguigu.gmallre.mock.log.enums.PageId;

import com.atguigu.mock.util.RandomNum;
import com.atguigu.gmallre.mock.service.CartInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("cart")
public class CartStage implements Stage{

    @Autowired
    CartInfoService cartInfoService;

    @Autowired
    LogService logService;

    @Override
    public  boolean handle() {
        AppMain appMain =  mainLog();


        handleCartChange(appMain);

        logService.doLog(appMain) ;

        UserSession.setLastPageId(PageId.cart);
        return true;
    }



    private  AppMain mainLog(  ){
        AppCommon appCommon = UserSession.getAppCommon();

        Integer durTime= RandomNum.getRandInt(5000, AppConfig.page_during_max_ms);

        AppPage appPage = AppPage.builder()
                .last_page_id(UserSession.getLastPageId())
                .during_time(durTime)
                .page_id(PageId.cart).buildFinal();


        List<AppDisplay> appDisplayList = AppDisplay.builder().pos_id(5).max_size(20).buildList();
        UserSession.setLastDisplay(appDisplayList);
        UserSession.addTimeByDuringTime(appPage.getDuring_time());
        AppMain appMain = AppMain.builder().common(appCommon).page(appPage).displays(appDisplayList).ts(UserSession.getCurDateTime().getTime()).checkError().build();
        return appMain;
    }

    private  void   handleCartChange(AppMain appMain){
        List<CartInfo> cartList = UserSession.getCartList();

        Boolean ifCartAdd=  RandomBox.builder().add(true ,AppConfig.if_cart_add_num_rate)
                .add(false,100-AppConfig.if_cart_add_num_rate).build().getRandBoolValue();
        UserSession.addTimeByDuringTime(2000);
        if(ifCartAdd){
            CartInfo cartInfo = RandomCollection.getOneFrom(cartList);
            cartInfo.setSkuNum(cartInfo.getSkuNum()+1);
            cartInfo.setOperateTime(UserSession.getCurDateTime());
            cartInfoService.saveOrUpdate(cartInfo);

            AppAction cartAction = new AppAction(ActionId.cart_add_num, ItemType.sku_id, cartInfo.getSkuId().toString());
            appMain.addAction(cartAction);
        }else {

            Boolean ifCartDel=  RandomBox.builder().add(true ,AppConfig.if_cart_rm_rate)
                    .add(false,100-AppConfig.if_cart_rm_rate).build().getRandBoolValue();
            if(ifCartDel){
                CartInfo cartInfo = RandomCollection.getOneFrom(cartList);
                cartInfoService.removeById(cartInfo.getId());

                cartList.remove(cartInfo);

                AppAction cartAction = new AppAction(ActionId.cart_remove, ItemType.sku_id, cartInfo.getSkuId().toString());
                appMain.addAction(cartAction);
            }



        }



    }




}
